home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 February / enter-2006-02.iso / files / Illustrator_CS2_ue_TryOut.exe / bridge / Adobe Bridge 1.0.msi / Data1.cab / _2document.jsx < prev    next >
Encoding:
Text File  |  2005-03-24  |  16.9 KB  |  614 lines

  1. /**************************************************************************
  2. *
  3. *  @@@BUILDINFO@@@ 12document.jsx 1.0.0.59 05-Mar-2005
  4. *  Copyright 2005 Adobe Systems Incorporated
  5. *  All Rights Reserved.
  6. *
  7. * NOTICE:  All information contained herein is, and remains the property of
  8. * Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
  9. * and technical concepts contained herein are proprietary to  Adobe Systems 
  10. * Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
  11. * Patents,patents in process,and are protected by trade secret or copyright 
  12. * law.  Dissemination of this  information or reproduction of this material
  13. * is strictly  forbidden  unless prior written permission is  obtained from 
  14. * Adobe Systems Incorporated.
  15. **************************************************************************/
  16.  
  17. // This file contains add-on functionality for the built-in Document object.
  18.  
  19. // Documents may have the following optional properties:
  20. // status - one of the following:
  21. //  exec        - executable
  22. //  noexec        - not executable
  23. //  active        - has been executed, waiting for callbacks in the target
  24. //  dynamic        - no script ID, came from target, not saveable & restartable
  25. //  tempBP        - a line number for a temporary breakpoint
  26.  
  27. // The script ID is one of the following:
  28. // (name)       - if the document is newly created
  29. // /path        - if the document has been saved into a file
  30. // id            - if the target app has supplied the ID
  31.  
  32. // Callback for a document after it has been created, but before it contains any text.
  33.  
  34. Document.prototype.onCreate = function()
  35. {
  36.     // the default
  37.     this.status = "exec";
  38.     // the last number of BPs sent to the target
  39.     this.lastBPCount = 0;
  40.     // TIGER (Mac OS 10.4) COLOR PROBLEM FIX: draw unbuffered on Tiger
  41.     var os = $.os;
  42.     if (os.indexOf ("Macintosh OS ") == 0)
  43.     {
  44.         if (parseFloat (os.substr (13)) > 10.3)
  45.             this.__flags__ |= 2;
  46.     }
  47.     // TIGER (Mac OS 10.4) COLOR PROBLEM FIX END
  48.     if (documents.length == 1)
  49.         debugMenu.reflectState();
  50. }
  51.  
  52. // Callback for a document after it has been loaded from a disk file.
  53.  
  54. Document.prototype.onLoad = function()
  55. {
  56.     this.addToRecentFiles();
  57. }
  58.  
  59. // Callback for a document when it is activated or deactivated.
  60.  
  61. Document.prototype.onActivate = function (on)
  62. {
  63.     // Update the Breakpoints pane
  64.     if (on)
  65.         window.breakpoints.update();
  66.     // Don't invalidate on deactivate, because deactivation occurs whenever
  67.     // current doc window loses focus.
  68. }
  69.  
  70. // Callback for documents to set the help tip by context
  71.  
  72. Document.prototype.onMouseOver = function (line, text)
  73. {
  74.     if (currentDebugger && prefs.helpTips && prefs.dynamicHelp)
  75.         currentDebugger.getHelpTip (this, line, text);
  76.     else
  77.         this.helpTip = "";
  78. }
  79.  
  80. // Called when the editor is about to be modified. Return false to deny the request.
  81.  
  82. Document.prototype.onModify = function()
  83. {
  84.     var ok = true;
  85.     // If we are debugging, we need to ask whether to stop
  86.     if (currentDebugger && currentDebugger.isActive())
  87.     {
  88.         if (!queryBox ("$$$/ESToolkit/Alerts/StopDebugging=Stop debugging?"))
  89.             return false;
  90.         // return false if there is a different text displayed
  91.         if (this.savedText)
  92.             ok = false;
  93.         currentDebugger.stop();
  94.     }
  95.     return ok;
  96. }
  97.  
  98. // Called whenever the Toolkit detects a change on disk that affects a loaded file.
  99. // Return false to tell the Toolkit that the disk image is different from the Toolkit's data.
  100.  
  101. Document.prototype.onModifiedFromOutside = function()
  102. {
  103.     var msg = "$$$/ESToolkit/Alerts/Modified1=File %1^nhas been changed outside of the Toolkit.^nDo you want to reload it?";
  104.     if (this.modified)
  105.         msg = "$$$/ESToolkit/Alerts/Modified2=File %1^nhas been changed outside of the Toolkit.^nDo you want to reload it and lose all your changes?";
  106.     var reload = prefs.autoReload || queryBox (msg, this.path.fsName);
  107.     if (reload)
  108.     {
  109.         var f = this.path;
  110.         if (f.open())
  111.         {
  112.             this.text = f.read();
  113.             f.close();
  114.         }
  115.         reload = (f.status == "");
  116.     }
  117.     return reload;
  118. }
  119.  
  120. // Called when the line count of the editor has changed.
  121.  
  122. Document.prototype.onLineNumbersChanged = function()
  123. {
  124.     // Update the Breakpoints pane, because the change in line #s
  125.     // may mean a line with a breakpoint has been deleted, or
  126.     // lines were inserted, changing the line numbers on existing BPs
  127.     window.breakpoints.update();
  128. }
  129.  
  130. // Callback for documents to set, disable, or clear a breakpoint
  131.  
  132. Document.prototype.onLineClick = function (line, count)
  133. {
  134.     // ignore the click count for now
  135.     this.toggleBP (line);
  136. }
  137.  
  138. // When a document is about to be saved, send the target a PutScript message
  139. // if there is a target and engine defined, and if the script ID is not a file.
  140. // Return true if a JS save operation was committed or initiated.
  141.  
  142. Document.prototype.onSave = function()
  143. {
  144.     return window.scripts.storeScript (this);
  145. }
  146.  
  147. // Callback notification when a document has been saved.
  148.  
  149. Document.prototype.onSaved = function()
  150. {
  151.     this.addToRecentFiles();
  152. }
  153.  
  154. // Ask if it is OK to close this document.
  155.  
  156. Document.prototype.queryClose = function()
  157. {
  158.     // if this doc is being debugged, ask to stop debugging
  159.     if (currentDebugger && currentDebugger.isActive() && currentDebugger.document == this)
  160.         return Debugger.queryExit();
  161.     else
  162.         return true;
  163. }
  164.  
  165. // When a document is about to close, deselect the Scripts pane.
  166.  
  167. Document.prototype.onClose = function()
  168. {
  169.     window.scripts.deselect();
  170.     currentDebugger.closeDocument (this);
  171.     if (documents.length == 1 && !currentDebugger.isActive())
  172.         // If there are no documents left, and no active debugger, disable debugging
  173.         // If there is an active debugger
  174.         debugMenu.disable();
  175.     // Update the Breakpoints pane
  176.     window.breakpoints.invalidate();
  177. }
  178.  
  179. // Add this document to the Recent Files list.
  180.  
  181. Document.prototype.addToRecentFiles = function()
  182. {
  183.     // If the doc is a file, append it to the Recent Files list
  184.     var f = new File (this.path);
  185.     if (f.exists)
  186.     {
  187.         var path = f.absoluteURI;
  188.         for (var i = 0; i < prefs.recentFiles.length; i++)
  189.         {
  190.             if (prefs.recentFiles [i] == path)
  191.             {
  192.                 // remove if present
  193.                 prefs.recentFiles.splice (i, 1);
  194.                 break;
  195.             }
  196.         }
  197.         if (prefs.recentFiles.length == 9)
  198.             prefs.recentFiles.pop();
  199.         // insert the new name at the top
  200.         prefs.recentFiles.splice (0, 0, path);
  201.     }
  202. }
  203.  
  204. ///////////////////////////////////////////////////////////////
  205.  
  206. // Breakpoints.
  207.  
  208. // Get a list of all breakpoints. This call returns an array if breakpoints
  209. // containing objects. Each objects contains a line property (number, zero-based),
  210. // a enabled property (true/false), and a condition property (string) if there
  211. // is a condition attached.
  212.  
  213. Document.prototype.getAllBP = function()
  214. {
  215.     var bps = [];
  216.     var lines = this.getFlaggedLines();
  217.     for (var i = 0; i < lines.length; i++)
  218.     {
  219.         var line = lines [i];
  220.         var flags = this.lineFlags [line] & Debugger.BP_MASK;
  221.         var obj = { line:line, enabled:((flags & Debugger.BP_ENABLED_MASK) != 0) };
  222.         var cond = this.lineData [line];
  223.         if (cond)
  224.             obj.condition = cond;
  225.         bps.push (obj);
  226.     }
  227.     return bps;
  228. }
  229.  
  230. // Is there a breakpoint at tyhe given line?
  231.  
  232. Document.prototype.isBP = function (line)
  233. {
  234.     return ((this.lineFlags [line] & Debugger.BP_MASK) != 0);
  235. }
  236.  
  237. // Restore a breakpoint; used when loading the prefs
  238.  
  239. Document.prototype.restoreBP = function (line, enabled, cond)
  240. {
  241.     var flags;
  242.     if (cond)
  243.         flags = enabled ? Debugger.BP_COND_ENABLED : Debugger.BP_COND_DISABLED;
  244.     else
  245.         flags = enabled ? Debugger.BP_ENABLED : Debugger.BP_DISABLED;
  246.     this.lineFlags [line] = flags;
  247.     if (cond)
  248.         this.lineData [line] = cond;
  249.     this.sendBP();
  250. }
  251.  
  252. // Delete a breakpoint including its condition.
  253.  
  254. Document.prototype.clearBP = function (line)
  255. {
  256.     if (this.lineFlags [line] & Debugger.BP_MASK)
  257.     {
  258.         this.lineFlags [line] &= ~Debugger.BP_MASK;
  259.         this.lineData [line] = null;
  260.         // Update the Breakpoints pane
  261.         window.breakpoints.update();
  262.         this.sendBP();
  263.     }
  264. }
  265.  
  266. // Toggle a breakpoint from off to enabled to disabled.
  267.  
  268. Document.prototype.toggleBP = function (line)
  269. {
  270.     var oldFlags = this.lineFlags [line];
  271.     var cond = this.lineData [line];
  272.     var flags = oldFlags & ~Debugger.BP_MASK;
  273.     switch (oldFlags & Debugger.BP_MASK)
  274.     {
  275.         case 0:
  276.             this.lineFlags [line] = flags | (cond ? Debugger.BP_COND_ENABLED : Debugger.BP_ENABLED);
  277.             break;
  278.         case Debugger.BP_COND_ENABLED:
  279.         case Debugger.BP_ENABLED:    
  280.             this.lineFlags [line] = flags | (cond ? Debugger.BP_COND_DISABLED : Debugger.BP_DISABLED);
  281.             break;
  282.         default:
  283.             this.lineFlags [line] = flags;
  284.     }
  285.     // Update the Breakpoints pane
  286.     window.breakpoints.update();
  287.     this.sendBP();
  288. }
  289.  
  290. // Remove all breakpoints and conditions.
  291.  
  292. Document.prototype.clearAllBP = function()
  293. {
  294.     var lines = this.getFlaggedLines();
  295.     for (var i = 0; i < lines.length; i++)
  296.     {
  297.         var line = lines [i];
  298.         this.lineFlags [line] = this.lineFlags [line] & ~Debugger.BP_MASK;
  299.         this.lineData  [line] = null;
  300.     }
  301.     if (lines.length)
  302.     {
  303.         // Update the Breakpoints pane
  304.         window.breakpoints.update();
  305.         this.sendBP();
  306.     }
  307. }
  308.  
  309. // Send an updated list of breakpoints to the target if the target is running.
  310.  
  311. Document.prototype.sendBP = function()
  312. {
  313.     if (currentDebugger && currentDebugger.state != Debugger.INACTIVE)
  314.     {
  315.         var bt = BridgeTalk.create (currentDebugger.target, "Breakpoints");
  316.         bt.headers.Engine = currentDebugger.engine;
  317.         bt.headers.ScriptID = this.scriptID;
  318.         this.attachBP (bt);
  319.         bt.safeSend();
  320.     }
  321. }
  322.  
  323. // Append all breakpoints for the document to the supplied BT message.
  324. // All breakpoints are stored in the header as BPx, where x counts from 1.
  325. // The BP info is the line number (0-based), a space, and a condition.
  326. // The header BPCount takes the number of BPs. If document.tempBP is defined, also
  327. // define a temporary BP at that line
  328.  
  329. Document.prototype.attachBP = function (bt)
  330. {
  331.     var lineFlags = this.lineFlags;
  332.     var bpCount = 0;
  333.     var tempBP = this.tempBP;
  334.     delete this.tempBP;
  335.  
  336.     var lines = this.getFlaggedLines();
  337.     for (var i = 0; i < lines.length; i++)
  338.     {
  339.         var line = lines [i];
  340.         var flags = lineFlags [line] & Debugger.BP_MASK;
  341.         if (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED)
  342.         {
  343.             // the temp BP matches a real BP, so forget about it
  344.             if (line == tempBP)
  345.                 tempBP = undefined;
  346.             var text = line + ",true";
  347.             var cond = this.lineData [line];
  348.             if (cond)
  349.                 text += "," + bt.encode (cond);
  350.             bt.headers ["BP"+ ++bpCount] = text;
  351.         }
  352.     }
  353.     // append a temp BP if given
  354.     if (tempBP != undefined)
  355.         bt.headers ["BP"+ ++bpCount] = tempBP + ",temp";
  356.     if (bpCount != this.lastBPCount)
  357.         bt.headers.BPClear = "1";
  358.     if (bpCount > 0)
  359.         bt.headers.BPCount = bpCount;
  360.     this.lastBPCount = bpCount;
  361. }
  362.  
  363. // Return a string containing all breakpoints as needed by the SetBreakpoints message.
  364. // The body of this message is formatted as follows:
  365. // script ID
  366. // line,state[,condition]   where state is true,false,temp
  367. // ...
  368. // <newline>
  369.  
  370. Document.prototype.createBPList = function()
  371. {
  372.     var lineFlags = this.lineFlags;
  373.     var bpCount = 0;
  374.     var tempBP = this.tempBP;
  375.     delete this.tempBP;
  376.     var reply = "";
  377.  
  378.     var lines = this.getFlaggedLines();
  379.     for (var i = 0; i < lines.length; i++)
  380.     {
  381.         var line = lines [i];
  382.         var flags = lineFlags [line] & Debugger.BP_MASK;
  383.         if (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED)
  384.         {
  385.             // the temp BP matches a real BP, so forget about it
  386.             if (line == tempBP)
  387.                 tempBP = undefined;
  388.             var text = line + ",true";
  389.             var cond = this.lineData [line];
  390.             if (cond)
  391.                 text += "," + bt.encode (cond);
  392.             reply += text + '\n';
  393.             bpCount++;
  394.         }
  395.     }
  396.     // append a temp BP if given
  397.     if (tempBP != undefined)
  398.         reply += tempBP + ",temp\n", bpCount++;
  399.     if (bpCount)
  400.         reply = this.scriptID + '\n' + reply + '\n';
  401.     return reply;
  402. }
  403.  
  404. // Evaluate the body of a Breakpoints message sent by the target.
  405. // This message contains a list of altered breakpoints, two numbers
  406. // on each line. If the breakpoint could not be set at all, the second
  407. // number is < 0.
  408.  
  409. Document.prototype.updateBP = function (bt)
  410. {
  411.     var reply = bt.splitBody();
  412.     for (var i = 0; i < reply.length; i++)
  413.     {
  414.         var oldLine = +reply [i][0];
  415.         var newLine = +reply [i][1];
  416.         var oldBP    = this.lineFlags [oldLine] & Debugger.BP_MASK;
  417.         var    oldCond    = this.lineData  [oldLine];
  418.         this.lineFlags [oldLine] &= ~Debugger.BP_MASK;
  419.         this.lineData  [oldLine] = null;
  420.         if (newLine >= 0)
  421.         {
  422.             this.lineFlags [newLine] = (this.lineFlags [newLine] & ~Debugger.BP_MASK) | oldBP;
  423.             this.lineData  [newLine] = oldCond;
  424.         }
  425.     }
  426.     // If any BP locations were changed, update the Breakpoints pane
  427.     if (reply.length)
  428.         window.breakpoints.update();
  429. }
  430.  
  431. // Write all breakpoints to the Prefs file.
  432.  
  433. Document.prototype.writeBP = function (f)
  434. {
  435.     var hdrWritten = false;
  436.     var lineFlags = this.lineFlags;
  437.     var lines = this.getFlaggedLines();
  438.     for (var i = 0; i < lines.length; i++)
  439.     {
  440.         var line = lines [i];
  441.         var flags = lineFlags [line] & Debugger.BP_MASK;
  442.         if (flags)
  443.         {
  444.             if (!hdrWritten)
  445.                 hdrWritten = true,
  446.                 f.write ('\tdoc = documents.find ("' + this.scriptID + '");\n\tif (doc) {\n');
  447.             f.write ('\t\tdoc.restoreBP (' + line + ', ',
  448.                     (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED));
  449.             var cond = this.lineData [line];
  450.             if (cond)
  451.                 f.write (", " + cond.toSource());
  452.             f.write (");\n");
  453.         }
  454.     }
  455.     if (hdrWritten)
  456.         f.write ("\t};\n");
  457. }
  458.  
  459. Document.prototype.setProfDisplayMode = function (mode)
  460. {
  461.     this.profileDisplayMode = mode;
  462. }
  463.  
  464. // clear the profiler data for this document
  465.  
  466. Document.prototype.clearProfData = function()
  467. {
  468.     this.profileData = null;
  469. }
  470.  
  471. /////////////////////////////////////////////////////////////////////////
  472.  
  473. // Documents collection.
  474.  
  475. // Set the profiler display data mode for all open documents.
  476.  
  477. documents.setProfDisplayMode = function (mode)
  478. {
  479.     for (var i = 0; i < documents.length; i++)
  480.         documents [i].profileDisplayMode = mode;
  481. }
  482.  
  483. // Clear the code profiler data for all documents.
  484.  
  485. documents.clearProfData = function()
  486. {
  487.     for (var i = 0; i < documents.length; i++)
  488.         documents [i].profileData = null;
  489. }
  490.  
  491. // Dump the profiler data into a file.
  492.  
  493. documents.saveProfData = function()
  494. {
  495.     var f = new File (Folder.current + "/profileData.csv");
  496.     f = f.saveDlg (
  497.             localize ("$$$/ESToolkit/ProfSaveDlg/Title=Save Profiler Data"),
  498.             localize ("$$$/ESToolkit/ProfSaveDlg/Types=CSV (Comma Delimited):*.csv"));
  499.  
  500.     if (f)
  501.     {
  502.         if (f.open ("w"))
  503.         {
  504.             for (var i = 0; i < documents.length; i++)
  505.             {
  506.                 var scriptID = documents [i].scriptID;
  507.                 if (scriptID[0] == '/' || scriptID [0] == '~')
  508.                 {
  509.                     var tmp = new File (scriptID);
  510.                     scriptID = tmp.fsName;
  511.                 }
  512.                 var data = documents [i].profileData;
  513.                 if (!data)
  514.                     continue;
  515.                 for (var j = 0; j < data.length; j++)
  516.                 {
  517.                     var line = data [j];
  518.                     f.write (scriptID + ',' + (line.line+1) + ',' + line.time + ',' + line.hits + '\n');
  519.                 }
  520.             }
  521.         }
  522.         if (f.error.length || !f.close())
  523.         {
  524.             errorBox ("$$$/ESToolkit/FileDlg/CannotWrite=Cannot write to file %1!", f.fsName);
  525.             f.remove();
  526.         }
  527.     }
  528. }
  529.  
  530. // Find a document by script ID.
  531.  
  532. documents.find = function (scriptID)
  533. {
  534.     for (var i = 0; i < documents.length; i++)
  535.     {
  536.         if (documents [i].scriptID == scriptID)
  537.             return documents [i];
  538.     }
  539.     return null;
  540. }
  541.  
  542. // Send all breakpoints we have so far to the target.
  543.  
  544. documents.sendBP = function()
  545. {
  546.     if (currentDebugger)
  547.     {
  548.         var body = "";
  549.         for (var i = 0; i < documents.length; i++)
  550.             body += documents [i].createBPList();
  551.         if (body.length)
  552.         {
  553.             var bt = BridgeTalk.create (currentDebugger.target, "SetBreakpoints");
  554.             bt.headers.Engine = currentDebugger.engine;
  555.             bt.headers.ScriptID = this.scriptID;
  556.             bt.headers.ClearBP = 1;
  557.             bt.body = body;
  558.             bt.onResult = function (bt)
  559.             {
  560.                 // interpret all moved breakpoints
  561.                 var reply = bt.splitBody();
  562.                 var doc = null;
  563.                 var bpChanged = false;
  564.                 var newDoc = true;
  565.                 for (var i = 0; i < reply.length; i++)
  566.                 {
  567.                     if (newDoc)
  568.                     {
  569.                         doc = documents.find (reply [i][0]);
  570.                         newDoc = false;
  571.                     }
  572.                     else if (!reply [i].length)
  573.                     {
  574.                         doc = null;
  575.                         newDoc = true;
  576.                     }
  577.                     if (doc)
  578.                     {
  579.                         var oldLine = +reply [i][0];
  580.                         var newLine = +reply [i][1];
  581.                         var oldBP    = doc.lineFlags [oldLine] & Debugger.BP_MASK;
  582.                         var    oldCond    = doc.lineData  [oldLine];
  583.                         doc.lineFlags [oldLine] &= ~Debugger.BP_MASK;
  584.                         doc.lineData  [oldLine] = null;
  585.                         if (newLine >= 0)
  586.                         {
  587.                             doc.lineFlags [newLine] = (doc.lineFlags [newLine] & ~Debugger.BP_MASK) | oldBP;
  588.                             doc.lineData  [newLine] = oldCond;
  589.                         }
  590.                         bpChanged = true;
  591.                     }
  592.                 }
  593.                 // If any BP locations were changed, update the Breakpoints pane
  594.                 if (bpChanged)
  595.                     window.breakpoints.update();
  596.                 this.destroy();
  597.             }
  598.             bt.safeSend();
  599.         }
  600.     }
  601. }
  602.  
  603. // Old dummy stuff to make old prefs files work
  604.  
  605. function Breakpoints (doc) 
  606. {
  607.     this.doc = doc;
  608. }
  609.  
  610. Breakpoints.prototype.set = function (line, enabled, cond)
  611. {
  612.     this.doc.restoreBP (line, enabled, cond);
  613. }
  614.